<?php

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;

class DefaultDb_Repositories_SequentialActivitiesRepository extends EntityRepository
{

    /**
     * Buscamos todos los puntos a visitar apartir de la actividad secuencial
     * @param integer $sequentialActivityId
     * @return array
     */
    public function getPackagesBySequentialActivity($sequentialActivityId)
    {
        $em = $this->getEntityManager();
        $conn = $em->getConnection();

        $query = '
            SELECT
                  pk2o.id
                , pk2o.namePackage
                , pk2o.num_package
                , weight
                , width
                , height
                , depth
                , IF(rp.point_id = o.pointBuyer_id, true, false) is_shipping
            FROM sequential_activities sa
            INNER JOIN m3_commerce_order o ON sa.order_id = o.id
            INNER JOIN package_to_order pk2o ON o.id = pk2o.order_id
            INNER JOIN route_points rp ON rp.route_id = rp.id
            -- WHERE sa.id = 40';
        $res = $conn->executeQuery($query);
        $res = $res->fetchAll();
        foreach($res as $key => $r)
        {
            $res[$key]['is_shipping'] = rand(0,1) == 1; //$r['is_shipping'] == '1';
            $res[$key]['status_shipping'] = rand(0,3);
        }
        return $res;
    }

}

